package com.chinasofti.zbest.web;

import com.chinasofti.zbest.dao.model.Product;
import com.chinasofti.zbest.service.IProductService;
import com.chinasofti.zbest.service.info.ProductInfo;
import com.chinasofti.zbest.web.form.QueryProductForm;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 商品 前端控制器
 * </p>
 *
 * @author zbest
 * @since 2019-05-28
 */
@Controller
@Slf4j
public class ProductController extends BaseController {

    private static Logger logger= LoggerFactory.getLogger(ProductController.class);

    /**
     * 搜索
     * @param p
     * @return
     */

    @Autowired
    private IProductService productService;


    @RequestMapping("product")
    public String productList(QueryProductForm form, String message, ModelMap modelMap){
        logger.debug(message);
        logger.info("查询商品，参数");
        try {
            List<Product> productList;
            productList=productService.search(form.getProductName());
            modelMap.addAttribute("productList",productList);
            modelMap.addAttribute("message",message);
        }catch(Exception e) {
            logger.info("查询异常");
        }
        return "search/search";
    }


    /**
     * 所有商品
     * 分类显示
     * @param modelMap
     * @return
     */
    @RequestMapping("paint")
    public String paintAll(ModelMap modelMap){

        /**
         * 根据商品编号所有商品后端
         */
        log.info("根据商品编号所有商品后端");
        List<ProductInfo> selProduct=productService.searchProduct(6);
//        System.out.println(selProduct);
        log.info("{}",selProduct);
        modelMap.addAttribute("selProduct",selProduct);

        /**
         * 根据商品类型名显示商品
         */
        log.info("根据商品类型名显示商品");
        List<ProductInfo> productInfos = productService.searchName(6);

        log.info("##{}##",productInfos);
        modelMap.addAttribute(productInfos);

        /**
         * 根据上架日期显示
         */
        log.info("根据上架日期显示商品");
        List<ProductInfo> search=productService.searchDate(4);
        log.info("{}",search);
        modelMap.addAttribute("search",search);

        /**
         * 根据类型Id显示商品
         */
        log.info("根据类型Id显示商品");
        List<ProductInfo> queryCategory=productService.searchCategoryId(1);
        modelMap.addAttribute("queryCategory",queryCategory);
        log.info("{}",queryCategory);

        return "product/paint";
    }
    /**
     * 墙式壁桂
     * @param modelMap
     * @return
     */
    @RequestMapping("walls")
    public String walls(ModelMap modelMap){

        /**
         * 根据商品编号所有商品后端
         */
        log.info("根据商品编号所有商品后端");
        List<ProductInfo> selProduct=productService.searchDate(3);
//        System.out.println(selProduct);
        log.info("{}",selProduct);
        modelMap.addAttribute("selProduct",selProduct);

        /**
         * 根据商品类型名显示商品
         */
        log.info("根据商品类型名显示商品");
        List<ProductInfo> productInfos = productService.searchName(3);
        log.info("##{}##",productInfos);
        modelMap.addAttribute("productInfos",productInfos);

        /**
         * 根据类型Id显示商品
         */
        log.info("墙式壁桂");
        List<ProductInfo> searchBz=productService.searchCategoryId(11);
        modelMap.addAttribute("searchBz",searchBz);
        log.info("{}",searchBz);

        return "product/walls";
    }
    /**
     * 抱枕靠垫
     * @param modelMap
     * @return
     */
    @RequestMapping("bzproList")
    public String bzproList(ModelMap modelMap){

        log.info("抱枕靠垫");
        List<ProductInfo> searchBz=productService.searchCategoryId(11);
        modelMap.addAttribute("searchBz",searchBz);
        log.info("{}",searchBz);

        return "product/bzproList";
    }

    /**
     *装饰摆件
     * @param modelMap
     * @return
     */
    @RequestMapping("flowerDer")
    public String flowerDer(ModelMap modelMap){

        log.info("装饰摆件");
        List<ProductInfo> searchFlower=productService.searchCategoryId(4);
        modelMap.addAttribute("searchFlower",searchFlower);
        log.info("{}","searchFlower");

        return "product/flowerDer";
    }

    /**
     *布艺装饰
     * @param modelMap
     * @return
     */
    @RequestMapping("decoration")
    public String decorate(ModelMap modelMap){

        log.info("布艺装饰");
        List<ProductInfo> searchDecorates=productService.searchCategoryId(9);
        modelMap.addAttribute("searchDecorates",searchDecorates);
        log.info("{}",searchDecorates);

        return "product/decoration";
    }
    /**
     *创意家居
     * @param modelMap
     * @return
     */
    @RequestMapping("idea")
    public String idea(ModelMap modelMap){

        /**
         * 根据分类id展示商品
         */
        log.info("创意家居");
        List<Product> searchIdea=productService.searchIdea(6);
        modelMap.addAttribute("searchIdea",searchIdea);
        log.info("{}",searchIdea);

        log.info("创意家居");
        List<ProductInfo> searchIdeas=productService.searchCategoryId(22);
        modelMap.addAttribute("searchIdeas1",searchIdeas.subList(0,4));
        modelMap.addAttribute("searchIdeas2",searchIdeas.subList(4,8));
        modelMap.addAttribute("searchIdeas3",searchIdeas.subList(8,12));
        modelMap.addAttribute("searchIdeas4",searchIdeas.subList(2,6));
        modelMap.addAttribute("searchIdeas5",searchIdeas.subList(10,14));
        modelMap.addAttribute("searchIdeas6",searchIdeas.subList(6,9));

        log.info("{}",searchIdeas);

        return "product/idea";
    }

    /**
     * 蜡艺香薰
     * @param modelMap
     * @return
     */
    @RequestMapping("perfume")
    public String perfume(ModelMap modelMap){

        /**
         * 根据分类id展示商品
         */
        log.info("根据分类id展示商品");
        List<ProductInfo> searchPerfume=productService.searchCategoryId(13);
        modelMap.addAttribute("searchPerfume",searchPerfume);
        log.info("{}",searchPerfume);
        /**
         * 根据销量展示商品
         */
        log.info("根据销量展示商品");
        List<ProductInfo> selectTop=productService.searchCategoryId(13);
        modelMap.addAttribute("selectTop",selectTop.subList(2,8));
        log.info("{}",selectTop);
        return "product/perfume";
    }


    /**
     *干花干艺
     * @param modelMap
     * @return
     */
    @RequestMapping("proList")
    public String proList(ModelMap modelMap){

        log.info("干花干艺");
        List<ProductInfo> serachProList=productService.searchCategoryId(5);
        modelMap.addAttribute("serachProList",serachProList);
        log.info("{}",serachProList);

        return "product/proList";
    }

    /**
     *花瓶花器
     * @param modelMap
     * @return
     */
    @RequestMapping("vase_proList")
    public String vaseProlist(ModelMap modelMap){

        log.info("花瓶花器");
        List<ProductInfo> vaseSearch=productService.searchCategoryId(6);
        modelMap.addAttribute("vaseSearch",vaseSearch);
        log.info("{}",vaseSearch);
        return "product/vase_proList";
    }
    /**
     *桌布摆件
     * @param modelMap
     * @return
     */
    @RequestMapping("zbproList")
    public String zbproList(ModelMap modelMap){

        log.info("桌布摆件");
        List<ProductInfo> searchZb=productService.searchCategoryId(10);
        modelMap.addAttribute("searchZb",searchZb);
        log.info("{}",searchZb);

        return "product/zbproList";
    }
    @RequestMapping("tosearch")
    public String toSearch(String sortBy,String inputInfo, RedirectAttributes arr){
        if(sortBy==null){
            sortBy="新品上市";
            arr.addFlashAttribute("inputInfo", inputInfo);
            arr.addFlashAttribute("sortBy", sortBy);
        }else {
            log.info("sortBy" + sortBy);
            log.info(inputInfo);
            arr.addFlashAttribute("inputInfo", inputInfo);
            arr.addFlashAttribute("sortBy", sortBy);
        }
        return "redirect:search";
    }

    @RequestMapping("search")
    public String search(@ModelAttribute("sortBy")String sortBy, @ModelAttribute("inputInfo")String inputInfo, ModelMap modelMap, RedirectAttributes arr){

        log.info("进来没"+":"+inputInfo);
        List<Product> productlist = productService.search(inputInfo,sortBy);
        arr.addFlashAttribute("inputInfo",inputInfo);
        arr.addFlashAttribute("sortBy",sortBy);
        arr.addFlashAttribute("productlist",productlist);
        log.info(productlist.toString());
        return "redirect:orderBy";
    }

    @RequestMapping("orderBy")
    public  String orderBy(@ModelAttribute("sortBy")String sortBy,@ModelAttribute("inputInfo")String inputInfo,@ModelAttribute("productlist")List productlist,ModelMap modelMap){
        log.info("sortBy+orderBy"+sortBy);
        HashMap<String, List<String>> classifyMap = new HashMap<>();
        List<String>CategoryNameList = productService.orderByCategoryName();
        List<String>driedFlowerTypeList = productService.orderByDriedFlowerType();
        List<String>putPositionList = productService.orderByPutPosition();
        List<String>fitList = productService.orderByFit();
        List<String>colorList = productService.orderByColor();
        classifyMap.put("基本类型",CategoryNameList);
        classifyMap.put("干花类型",driedFlowerTypeList);
        classifyMap.put("摆放位置",putPositionList);
        classifyMap.put("适合对象",fitList);
        classifyMap.put("颜色",colorList);
        modelMap.addAttribute("classifyMap",classifyMap);
        modelMap.addAttribute("productlist",productlist);
        modelMap.addAttribute("inputInfo",inputInfo);
        return "search/search";
    }

    @RequestMapping("classifySearch")
    @ResponseBody
    public String classifySearch(String chooseInfo,ModelMap modelMap){

        return "search/search";
    }

}

